/**
* project3D Engine
* Texture Material
* @author John Sword
* @version 2 - AS3
*/

package engine.materials
{
	
	import engine.geom.Face;
	import engine.geom.Vector;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	
	
	public class Texture extends Material
	{

		public var smooth:Boolean = false;
		public var useWire:Boolean = false;

		public function Texture ( tex:BitmapData, flip:Boolean = true )
		{
			super( tex );
			if(flip)
			{
				// flip texture
				// needs a temp mc to apply the invert matrix
				var texMc:Sprite = new Sprite();
				var bmp:Bitmap = new Bitmap ( tex );
				// apply texture to temp mc
				texMc.addChild( bmp );
				// apply invert
				var textureMatrix:Matrix = new Matrix();
				textureMatrix.a = 1;
				textureMatrix.b = 0;
				textureMatrix.c = 0;
				textureMatrix.d = -1;
				textureMatrix.tx = 0;
				textureMatrix.ty = tex.height;
				// recreate texture
				texture = new BitmapData(texMc.width, texMc.height, true, 0x00000000);
				texture.draw(texMc, textureMatrix);
			} else {
				texture = tex;
			}
		}
		
		public override function render ( face:Face, screen:Sprite ) : void
		{
			//var v1:Vector = face.v1.screen;
			//var v2:Vector = face.v2.screen;
			//var v3:Vector = face.v3.screen;

			var g:Graphics = screen.graphics;

			g.beginBitmapFill( texture, face.getTextureMatrix(), false, smooth );
			if ( useWire ) g.lineStyle( wireSize, wireColor, wireAlpha);
			//g.lineStyle( 0, 0, 0);
			// Draw triangle
			//g.moveTo( v1.x, v1.y );
			//g.lineTo( v2.x, v2.y );
			//g.lineTo( v3.x, v3.y );
			//g.endFill();*/
			g.moveTo( face.v1.screen.x, face.v1.screen.y );
			g.lineTo( face.v2.screen.x, face.v2.screen.y );
			g.lineTo( face.v3.screen.x, face.v3.screen.y );
			g.endFill();
		}
		
	}
	
}
